package repositorios;

import excessoes.DAOException;
import interfaces.InterfaceVolumeProcessoDAO;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.ConstraintViolationException;
import entidades.VolumeProcesso;

/**
 *
 * @author Marcelo Gomes Neto
 */
public class VolumeProcessoDAOHibernate implements InterfaceVolumeProcessoDAO 
{
    private Session session;

    public void setSessao(Session session) {
        this.session = session;
    }

    @Override
    public VolumeProcesso carregarVolume(Integer id) {
        return (VolumeProcesso) this.session.get(VolumeProcesso.class, id);
    }

    @Override
    public void excluirVolume(VolumeProcesso volumeProcesso) {
        this.session.delete(volumeProcesso);
    }

    @Override
    public void incluirVolume(VolumeProcesso volumeProcesso) throws DAOException 
    {
        try {
           this.session.save(volumeProcesso);
        }
        catch (ConstraintViolationException e) {
            throw new DAOException("Erro de chave duplicada\n" + e.getConstraintName());
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<VolumeProcesso> listarVolume() 
    {
        Criteria criteria = this.session.createCriteria(VolumeProcesso.class);
        criteria.addOrder((Order.desc("idprocesso")));
        criteria.addOrder((Order.asc("idvolume")));
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<VolumeProcesso> listarVolumesPorProcesso(int idprocesso, int idvolume)
    {
        Criteria criteria = session.createCriteria(VolumeProcesso.class);
        criteria.add(Restrictions.eq("idprocesso", idprocesso));
        criteria.add(Restrictions.eq("idvolume", idvolume));
        criteria.addOrder((Order.asc("idvolume")));
        //this.session.clear();
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<VolumeProcesso> listarVolumesPorProcesso(int idprocesso)
    {
        Criteria criteria = session.createCriteria(VolumeProcesso.class);
        criteria.add(Restrictions.eq("idprocesso", idprocesso));
        criteria.addOrder((Order.asc("idvolume")));
        //this.session.clear();
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public VolumeProcesso carregarVolumePorProcesso(int idprocesso, int idvolume)
    {
        Criteria criteria = session.createCriteria(VolumeProcesso.class);
        criteria.add(Restrictions.eq("idprocesso", idprocesso));
        criteria.add(Restrictions.eq("idvolume", idvolume));
        //this.session.clear();
        return (VolumeProcesso) criteria.uniqueResult();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<VolumeProcesso> listarVolumesNoSetor(int unidadeAtual, int setorAtual)
    {
        Criteria criteria = session.createCriteria(VolumeProcesso.class);
        criteria.createAlias("processo", "p");
	criteria.add(Restrictions.eq("p.status", 1));
        criteria.add(Restrictions.eq("unidadeAtual", unidadeAtual));
        criteria.add(Restrictions.eq("setorAtual", setorAtual));
        criteria.addOrder((Order.desc("idvolume")));
        //this.session.clear();
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public Object totalVolumesNoSetor(int unidadeAtual, int setorAtual)
    {
        //String hql="select count(*) from volume_processo where status = "+status;
        String hql="select count(*) from volume_processo vp, processo p where p.idprocesso=vp.idprocesso and p.status = 1 and vp.unidade_atual = "+unidadeAtual+" and vp.setor_atual = "+setorAtual;
        Query consulta = this.session.createSQLQuery(hql);
        return consulta.uniqueResult();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public Object totalVolumesPorStatus(int status)
    {
        String hql="select count(*) from volume_processo vp, processo p where p.status = "+status;
        Query consulta = this.session.createSQLQuery(hql);
        return consulta.uniqueResult();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<VolumeProcesso> listarVolumesPorStatus(int status)
    {
        Criteria criteria = session.createCriteria(VolumeProcesso.class);
        criteria.createAlias("processo", "p");
        criteria.add(Restrictions.eq("p.status", status));
        criteria.addOrder((Order.desc("idprocesso")));
        criteria.addOrder((Order.asc("idvolume")));
        return criteria.list();
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<VolumeProcesso> listarVolumesPorPeriodo(Date dataIni, Date dataFim, int status)
    {
        Criteria criteria = this.session.createCriteria(VolumeProcesso.class);
        criteria.createAlias("processo", "p");
        criteria.add(Restrictions.between("p.dataProcesso", dataIni, dataFim));  
        criteria.add(Restrictions.eq("p.status", status));
        criteria.addOrder((Order.desc("idprocesso")));
        criteria.addOrder((Order.asc("idvolume")));
        return criteria.list();
    }
}
